package fr.sportsurbains.reader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.util.Log;

public class TopicsReader {
	Connection cnx;
	Document doc;
	
	public TopicsReader (String url) throws IOException {
		cnx = Jsoup.connect(url);
		doc = cnx.timeout(10000).get();
	}
	
	public List<Map<String,String>> getTopics() {
		Elements elts = doc.getElementsByTag("article");
/*		if(elts.isEmpty()) {
			return null;
		}*/
		List<Map<String,String>> listTopics = new ArrayList<Map<String,String>>();
		for(Element elt:elts) {
			//Log.d("TEST",elt.className());
			if(elt.id().equals("post-0")) {
				//on mode search, there is no results
				break;
			}
			String urlImage = "";
			String summary = "";
			//String title = elt.getElementsByTag("header").first().getElementsByTag("h2").first().getElementsByTag("a").first().text();
			String title = elt.getElementsByTag("header").first().getElementsByTag("h3").first().getElementsByTag("a").first().text();
			/*if(elt.getElementsByClass("featured-image").size() > 0) {
				urlImage = elt.getElementsByClass("featured-image").first().getElementsByTag("a").first().getElementsByTag("img").attr("src");
				urlImage = "http:"+urlImage;//add prefix http
			}*/
			if(elt.getElementsByClass("loop-thumb").size() > 0) {
				urlImage = elt.getElementsByClass("loop-thumb").first().getElementsByTag("a").first().getElementsByTag("img").attr("src");
			}
			/*if(elt.getElementsByClass("entry-summary").size() > 0) {
				summary = elt.getElementsByClass("entry-summary").first().getElementsByTag("p").text();
				//limit the length of summary
				if(summary.length() > 120) {
					summary = summary.substring(0,119)+"...";
				}
			}*/
			if(elt.getElementsByClass("loop-excerpt").size() > 0) {
				summary = elt.getElementsByClass("loop-excerpt").first().getElementsByTag("p").text();
				//limit the length of summary
				if(summary.length() > 120) {
					summary = summary.substring(0,119)+"...";
				}
			}
//			String id = elt.attr("id");
			String id = (elt.className().split(" "))[0];
			String date = "";
			/*if(elt.getElementsByTag("time").size()>0) {
				date = elt.getElementsByTag("time").first().attr("datetime");
			}*/
			if(elt.select("p[class=meta]").size()>0) {
				date = elt.select("p[class=meta]").first().getElementsByTag("a").first().text();
			}
//			String link = elt.getElementsByTag("header").first().getElementsByTag("h2").first().getElementsByTag("a").first().absUrl("href");
			String link = elt.getElementsByTag("header").first().getElementsByTag("h3").first().getElementsByTag("a").first().absUrl("href");
			Map<String,String> mp = new HashMap<String,String>();
			Log.v("TEST", "title "+title);
			Log.v("TEST", "image "+urlImage);
			Log.v("TEST", "summary "+summary);
			Log.v("TEST", "id "+id);
			Log.v("TEST", "date "+date);
			Log.v("TEST", "link "+link);


			mp.put("title", title);
			mp.put("image", urlImage);
			mp.put("summary", summary);
			mp.put("id", id);
			mp.put("date", date);
			mp.put("link", link);
			listTopics.add(mp);
		}
		return listTopics;
	}
	
	public List<Map<String,String>> getInfoEnContinu() {
		List<Map<String,String>> listTopics = new ArrayList<Map<String,String>>();
		Elements elts = doc.getElementsByClass("sb-widget");
		for(Element elt:elts) {
			if(elt.getElementsByClass("widget-title").isEmpty()) {
				continue;
			}
			if(elt.getElementsByClass("widget-title").first().text().equals("> L’info en continu")){
				
				ListIterator<Element> ittitle = elt.getElementsByTag("a").listIterator();
				ListIterator<Element> itimg = elt.getElementsByAttribute("style").listIterator();
				ListIterator<Element> itsum = elt.getElementsByTag("p").listIterator();
				while(ittitle.hasNext()) {
					Element a = ittitle.next();
					String title = a.attr("title");
					String summary = "";
					while(itsum.hasNext()) {
							summary = itsum.next().text();
							if((!summary.startsWith(" "))&&(!summary.isEmpty())) {
								Log.v("TEST", "summary: "+summary);
								break;
							}
					}
					String img = "";
					if(itimg.hasNext()) {
						Element div = itimg.next();
						if(!(div.getElementsByTag("img").isEmpty())) {
							img = div.getElementsByTag("img").first().attr("src");
							Log.v("TEST", "image: "+img);
						}
					}
					String link = a.absUrl("href");
					Map<String,String> mp = new HashMap<String,String>();
					mp.put("title", title);
					mp.put("image", img);
					mp.put("summary", summary);
					mp.put("link", link);
					listTopics.add(mp);
				}
			}
		}
		return listTopics;
		
	}
	
	public List<Map<String,String>> getVideos() {
		List<Map<String,String>> listTopics = new ArrayList<Map<String,String>>();
		Elements elts = doc.getElementsByClass("col-2-3").first().getElementsByClass("avs_thumb");
		for(Element elt:elts) {
			String title = elt.getElementsByClass("title").first().text();
			String img = elt.getElementsByClass("image").first().attr("src");
			String summary = elt.getElementsByClass("hits").first().text();
			String link = elt.getElementsByTag("a").first().absUrl("href");
			Map<String,String> mp = new HashMap<String,String>();
			mp.put("title", title);
			mp.put("image", img);
			mp.put("summary", summary);
			mp.put("link", link);
			listTopics.add(mp);
		}
		return listTopics;
	}
	
	public String getNextUrl() {
		//because there is a blank space in the class, so use the method select
//		Element elt = doc.select("div[class=span6 next-post]").first().getElementsByTag("a").first();
		Element elt = doc.select("a[class=soliloquy-next]").first();
		if(elt == null) {
			return null;
		} else {
			if(elt.absUrl("href").equals("#")) {
				return null;
			}
			return elt.absUrl("href");
		}
	}
	public String getNextUrlInSearch() {
		//because there is a blank space in the class, so use the method select
//		Element elt = doc.select("div[class=span6 next-post]").first().getElementsByTag("a").first();
		Elements elts = doc.select("a[class=next page-numbers]");
		if(elts.isEmpty()) {
			return null;
		} else {
			return elts.first().absUrl("href");
		}
	}

}
